package com.course.cases;

import com.alibaba.fastjson.JSON;
import com.course.config.TestConfig;
import com.course.model.UpUser;
import com.course.model.User;
import com.course.utils.DatabaseUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;

/**
 * @author bigFish
 * @Title: public:
 */
public class UpController {
//    private SqlSession sqlSession;
//
//    {
//        try {
//            sqlSession = DatabaseUtil.getSqlSession();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Test(dependsOnGroups = "loginTrue", description = "更新用户信息")
    public void upUser() throws IOException, InterruptedException {
        SqlSession  sqlSession = DatabaseUtil.getSqlSession();
        //1.获取用例
        UpUser updateUser = sqlSession.selectOne("updateUserInfo", 1);
        System.out.println("用例信息：" + updateUser);
        System.out.println("请求地址:" + TestConfig.upUserUrl);

        //2.发起请求获取响应结果
        String result = getResult(updateUser);
        System.out.println("请求结果:"+result);

        //3.本地查询结果
        User user =  sqlSession.selectOne(updateUser.getExpected(), updateUser);
        System.out.println("本地查询结果："+user);

        //4.验证结果
        Assert.assertNotNull(user);
        Assert.assertEquals(result,"操作成功");
    }

    @Test(dependsOnGroups = "loginTrue", description = "删除用户信息")
    public void dlUser() throws IOException, InterruptedException {
        SqlSession  sqlSession = DatabaseUtil.getSqlSession();
        //2.获取用例
        UpUser updateUser = sqlSession.selectOne("updateUserInfo", 2);
        System.out.println("用例信息：" + updateUser);
        System.out.println("请求地址:" + TestConfig.upUserUrl);

        //2.发起请求获取响应结果
        String result = getResult(updateUser);
        System.out.println("请求结果："+result);

        //3.本地查询结果
        User user = sqlSession.selectOne(updateUser.getExpected(), updateUser);
        System.out.println("本地查询结果："+user);

        //4.验证结果
        Assert.assertNotNull(user);
        Assert.assertEquals(result,"操作成功");
    }

    private String getResult(UpUser updateUser) throws IOException {
        //1.设置请求类型
        HttpPost httpPost = new HttpPost(TestConfig.upUserUrl);
        //2.设置请求头
        httpPost.setHeader("content-type","application/json");
        //3.设置参数
        updateUser.setId(updateUser.getUserId());
        String s = JSON.toJSONString(updateUser);
        StringEntity entity = new StringEntity(s, "utf-8");
        httpPost.setEntity(entity);
        //4.发起请求
        CloseableHttpResponse response = TestConfig.getHttpClient().execute(httpPost);
        //5.获取响应结果实体
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        return result;
    }

}
